home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7459 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: risc.uni-linz.ac.at!jsixt
  2. From: jsixt@risc.uni-linz.ac.at (Johannes Sixt)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: const wasting space and slower?
  5. Date: 23 Feb 1996 11:52:23 GMT
  6. Organization: RISC, J.K. University of Linz, Austria
  7. Distribution: world
  8. Message-ID: <4gk9pn$r9i@alijku06.edvz.uni-linz.ac.at>
  9. References: <312814DB.399F@iastate.edu>
  10. NNTP-Posting-Host: melmac.risc.uni-linz.ac.at
  11.  
  12. In article <312814DB.399F@iastate.edu>, Steve Lee <sjlee@iastate.edu> writes:
  13. > When you declare something like this:
  14. > const int MAX_FILES = 20;
  15. > doesn't the compiler reserve global, read-only memory for this variable?  Or does it just replace 
  16. > instances in the source code where MAX_FILES is found and replaces it with 20, similar to a 
  17. > preprocessor, but with typechecking?  Also, if the compiler does place it in memory, do 
  18. > instructions then reference the memory, instead of immediate addressing mode?
  19.  
  20. According to the Working Paper const variables get "internal linkage" unless
  21. explicitly declared extern. "internal linkage" means that the variable's name can
  22. be accessed only within the current compilation unit. "external linkage", OTOH,
  23. as is assigned to (amongst others) functions by default, would mean that the name
  24. can be accessed by other compilation units.
  25.  
  26. Sooo... if nobody outside the current compilation unit is able to access the
  27. const variable, the compiler has the opportunity to optimize such that no memory
  28. will be allocated for it. Most probably a compiler will allocate memory and
  29. reference the memory in non-optimizing compilations, and will not allocate memory
  30. in high-optimization compilations.
  31.  
  32. If you define a const variable in a header file, every module which #includes the
  33. file will have its own copy of the variable. But this is ok, the value is the
  34. same in all units and can't be changed. If you edit the header and define a
  35. different value for the variable, you have to make sure that all modules
  36. referencing the header file get recompiled. But good development environmments
  37. will do this for you (e.g. Makefiles with proper dependencies).
  38.  
  39. -- Hannes
  40.  
  41. PS: Please stick to <80 chars/line!
  42.